<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/house/new-house' }">房源管理</el-breadcrumb-item>
      <el-breadcrumb-item>修改新房源</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <div style="width: 60%;">
      <el-form-item label="标题" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      </div>
      <div style="width: 30%;">
      <el-form-item label="价格" prop="name">
        <el-input v-model="ruleForm.price">
          <template slot="append">元/月</template>
        </el-input>
      </el-form-item>
      </div>
      <div style="width: 30%;">
      <el-form-item label="面积" prop="area">
        <el-input v-model="ruleForm.area">
          <template slot="append">平方米</template>
        </el-input>
      </el-form-item>
      </div>
      <div style="width: 40%;">
      <el-form-item label="户型" prop="houseType">
        <el-input v-model="ruleForm.houseType"></el-input>
      </el-form-item>
      </div>
      <div style="width: 20%;">
      <el-form-item label="楼层" prop="floor">
        <el-input v-model="ruleForm.floor">
          <template slot="append">层</template>
        </el-input>
      </el-form-item>
      </div>
      <div style="width: 20%;">
      <el-form-item label="最高楼层" prop="maxFloor">
        <el-input v-model="ruleForm.maxFloor">
          <template slot="append">层</template>
        </el-input>
      </el-form-item>
      </div>
      <div style="width: 70%;">
      <el-form-item label="小区简介" prop="communityDescription">
        <el-input v-model="ruleForm.communityDescription"></el-input>
      </el-form-item>
      </div>
      <el-form-item label="是否有电梯" prop="hasElevator">
        <el-radio v-model="ruleForm.hasElevator" :label="1">有</el-radio>
        <el-radio v-model="ruleForm.hasElevator" :label="0">无</el-radio>
      </el-form-item>
<!--      <el-form-item label="是否为推荐">-->
<!--        <el-switch-->
<!--            v-model="ruleForm.recom"-->
<!--            :active-value="1"-->
<!--            :inactive-value="0"-->
<!--            active-color="#13ce66"-->
<!--            inactive-color="#999">-->
<!--        </el-switch>-->
<!--      </el-form-item>-->
      <div style="width: 40%;">
      <el-form-item label="区域" prop="sectionId">
        <el-cascader
            style="width: 100%;"
            placeholder="请选择区域"
            v-model="ruleForm.sectionId"
            :options="sectionList">
        </el-cascader>
      </el-form-item>
      </div>
<!--      <el-form-item label="区域" prop="sectionId">-->
<!--        <el-select v-model="ruleForm.sectionId" placeholder="请选择区域" style="width: 300px;">-->
<!--          <el-option-->
<!--              v-for="item in sectionList"-->
<!--              :key="item.id"-->
<!--              :label="item.name"-->
<!--              :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="朝向" prop="orientationId">
        <el-select v-model="ruleForm.orientationId" placeholder="请选择朝向" style="width: 300px;">
          <el-option
              v-for="item in orientationList"
              :key="item.id"
              :label="item.direction"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="经纪人" prop="agentId">
        <el-select v-model="ruleForm.agentId" placeholder="请选择经纪人" style="width: 300px;">
          <el-option
              v-for="item in agentList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开盘时间" prop="communityDescription">
        <div class="block">
          <el-date-picker
              v-model="ruleForm.openingTime"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </div>
      </el-form-item>
      <div style="width: 30%;">
      <el-form-item label="交房时间" prop="communityDescription">
        <div class="block">
          <el-date-picker
              v-model="ruleForm.timeOfDelivery"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
          </el-date-picker>
        </div>
      </el-form-item>
      </div>
      <div style="width: 20%;">
      <el-form-item label="产权年限" prop="termOfOwnership">
        <el-input v-model="ruleForm.termOfOwnership">
          <template slot="append">年</template>
        </el-input>
      </el-form-item>
      </div>

      <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roleListOptions: [],
      ruleForm: {
        id: '',
        name: '',
        price: '',
        houseType: '',
        area: '',
        communityDescription: '',
        floor: '',
        hasElevator: '',
        maxFloor: '',
        recom: '',
        sectionId: '',
        orientationId: '',
        agentId: '',
        houseCategoryId: '1',
        openingTime: '',
        majorImage: '',
        houseImage:'',
        timeOfDelivery:''
      },
      rules: {
        name:[
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '请输入价格', trigger: 'blur'},
          {pattern: /^([1-9]\d*|0)(\.\d{1,2})?$/, message: '请输入正确价格', trigger: 'blur'}
        ],
        houseType: [
          {required: true, message: '请输入户型', trigger: 'blur'},
        ],
        area: [
          {required: true, message: '请输入面积', trigger: 'blur'}
        ],
        communityDescription: [
          {required: true, message: '请输入小区简介', trigger: 'blur'},
          {min: 4, max: 99, message: '长度在 4 到 99 个字符', trigger: 'blur'}
        ],
        floor: [
          {required: true, message: '请输入楼层', trigger: 'blur'},
          {pattern:/^[1-9]\d*$/,message: '请输入正确楼层',trigger: 'blur'}
        ],
        hasElevator: [
          {required: true, message: '请选择是否有电梯', trigger: 'blur'}
        ],
        maxFloor: [
          {required: true, message: '请输入最高楼层', trigger: 'blur'},
          {pattern:/^[1-9]\d*$/,message: '请输入正确楼层',trigger: 'blur'}
        ],
        recom: [
          {required: true, message: '请选择是否推荐', trigger: 'blur'}
        ],
        sectionId: [
          {required: true, message: '请选择区域', trigger: 'blur'}
        ],
        orientationId: [
          {required: true, message: '请选择朝向', trigger: 'blur'}
        ],
        agentId: [
          {required: true, message: '请选择经纪人', trigger: 'blur'}
        ],
        termOfOwnership: [
          {required: true, message: '请输入产权年限', trigger: 'blur'},
          {pattern:/^[1-9][0-9]?$|^70$/,message: '请输入正确产权年限',trigger: 'blur'}
        ],
        openingTime: [
          {required: true, message: '请输入开盘时间', trigger: 'blur'}
        ],
        timeOfDelivery: [
          {required: true, message: '请输入交房时间', trigger: 'blur'}
        ],
        houseImage: [
          {required: true, message: '请选择房源图片', trigger: 'blur'}
        ],
        majorImage: [
          {required: true, message: '请选择房源主图', trigger: 'blur'}
        ]
      },
      sectionList: [],
      orientationList: [],
      agentList: [],
      leaseTypeList: []
    };
  },
  methods: {
    submitForm(formName) {
      if (Array.isArray(this.ruleForm.sectionId)) {
        this.ruleForm.sectionId = this.ruleForm.sectionId.pop();
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
          this.$router.push('/house/new-house/update-2?houseCategoryId=4')
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    loadSection() {
      let url = 'http://localhost:9200/section/tree'
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.sectionList = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    loadOrientation() {
      let url = 'http://localhost:9200/orientation/list'
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.orientationList = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    loadAgentList() {
      let url = 'http://localhost:9200/agent/list?query=all'
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.agentList = jsonResult.data.list
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },

    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);

      if (!localRuleForm) {
        return;
      }

      this.ruleForm = localRuleForm;
    },
    loadNewHouseStandard() {
      let houseId = this.$router.currentRoute.query.id;
      let url = 'http://localhost:9200/house/standard/new-house?id=' + houseId;
      this.axios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.ruleForm = jsonResult.data
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadNewHouseStandard();
    this.loadLocalRuleForm();
    this.loadSection();
    this.loadOrientation();
    this.loadAgentList();
  }
}
</script>